﻿Imports TreeCreeper01Interfaces

Friend Class cNonSpatialAssistant

    Private pAbstractDataAgent As cAbstractDataAgent
    Private pAbstractReader As cAbstractReader

    Private pPlotDataColumnScanStartIndex As Integer
    Private pPlotDataColumnScanEndIndex As Integer


    Public Sub New(ByVal AbstractDataAgent As cAbstractDataAgent)
        pAbstractDataAgent = AbstractDataAgent
        pAbstractReader = AbstractDataAgent.AbstractReader
        InferMetaInfo()
    End Sub


    Private Sub InferMetaInfo()

        'Here is the logic of the data structure for non spatial data sets
        With pAbstractReader
            If .DoesHaveNumericFirstColumn Then
                pPlotDataColumnScanStartIndex = .EstimatedNumberOfTaxonomicColumns + 2
            Else
                pPlotDataColumnScanStartIndex = .EstimatedNumberOfTaxonomicColumns + 1
            End If
            pPlotDataColumnScanEndIndex = pPlotDataColumnScanStartIndex + .EstimatedNumberOfRightAlignedNumericColumns - 1
            Debug.Assert(.NumberOfColumns = pPlotDataColumnScanEndIndex)

            pAbstractDataAgent.TaxonomicDataColumnScanEndIndex = .EstimatedNumberOfTaxonomicColumns
            If .DoesHaveNumericFirstColumn Then
                pAbstractDataAgent.TaxonomicDataColumnScanStartIndex = 2
                pAbstractDataAgent.TaxonomicDataColumnScanEndIndex = pAbstractDataAgent.TaxonomicDataColumnScanEndIndex + 1
            Else
                pAbstractDataAgent.TaxonomicDataColumnScanStartIndex = 1
            End If
        End With

    End Sub


    Public Sub PopulateStudyNonSpatial(ByVal Study As IStudyNonSpatial)
        pAbstractDataAgent.PopulateTaxonomicTree(CType(Study.TaxonomicTree, ITaxonomicTree))
        pAbstractDataAgent.PopulateSpeciesAbundances(Study.Abundances, Study.TaxonomicTree.AllSpecies)
        PopulateSampledPlots(Study.SampledPlots)
    End Sub

    Private Sub PopulateSampledPlots(ByVal SampledPlots As IListFilterCollection)

        Dim ListFilter As IListFilter
        Dim ColumnIndex As Integer
        Dim RowIndex As Integer
        Dim CellData As Double?
        Dim SpeciesUniqueKey As String
        Dim ErrorText As String

        For ColumnIndex = pPlotDataColumnScanStartIndex To pPlotDataColumnScanEndIndex

            ListFilter = SampledPlots.AddListFilter(pAbstractReader.GetStringValue(1, ColumnIndex))

            For RowIndex = IndexOfFirstDataRow To pAbstractReader.NumberOfRows
                CellData = pAbstractReader.ReadDoubleValue(RowIndex, ColumnIndex)
                If CellData > 0 Then
                    SpeciesUniqueKey = pAbstractDataAgent.GenerateUniqueKeyForSpecies(RowIndex)
                    AddNonSpatialListFilterItemToListFilter(ListFilter, SpeciesUniqueKey, CDbl(CellData))
                End If
            Next
        Next

        ErrorText = pAbstractReader.ErrorTextWhenEvaluatingCells
        If ErrorText <> "" Then
            Throw New Exception(ErrorText)
        End If

    End Sub

End Class
